//
//  LYRecomedController.m
//  SelectCity
//
//  Created by liuyan on 2019/3/13.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYRecomedController.h"
#import "LYRecomendCell.h"

@interface LYRecomedController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, assign) NSInteger index;
@end

@implementation LYRecomedController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBA(0, 0, 0, 0.2);
    self.index = 0;
    [self createSubview];
}

- (void)createSubview{
    CGFloat height = 0;
    if (self.dataArray && self.dataArray.count) {
        height = self.dataArray.count*45;
    } else {
        height = self.itemDataArray.count*45;
    }
    if (height > 300) {
        height = 300;
    }
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, height) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.showsVerticalScrollIndicator = NO;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.backgroundColor = [UIColor whiteColor];
    self.tableview.sectionIndexBackgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableview];
    
    [self.tableview registerClass:[LYRecomendCell class] forCellReuseIdentifier:kCellIdentifier_LYRecomendCell];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataArray && self.dataArray.count) {
        return self.dataArray.count;
    }
    return self.itemDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYRecomendCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYRecomendCell forIndexPath:indexPath];
    if (self.dataArray && self.dataArray.count) {
        [cell setDataDic:self.dataArray[indexPath.row] isSelect:((self.index == indexPath.row)?YES:NO)];
    } else {
        [cell reloadData:self.itemDataArray[indexPath.row] isSelect:((self.index == indexPath.row)?YES:NO)];
    }
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.index = indexPath.row;
    [tableView reloadData];
    if (self.dataArray && self.dataArray.count) {
        NSDictionary *dic = self.dataArray[indexPath.row];
        _block(dic);
    } else {
        PublicCategoryElementModel *model = self.itemDataArray[indexPath.row];
        self.backBlock(model);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}

@end
